package org.hackystat.devcathlon.ui.page.teams.manage;

import static org.junit.Assert.assertNotNull;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.hackystat.devcathlon.engine.team.TeamManager;
import org.hackystat.devcathlon.engine.test.DevcathlonTestHelper;
import org.hackystat.devcathlon.ui.DevcathlonApplication;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.login.LoginPage;
import org.junit.Test;

/**
 * Test classes for the BestCoverage starter event. 
 *
 * @author John Ly
 *
 */

public class TestTeamsManageDeletePage extends DevcathlonTestHelper { 
     
  private String user = "admin@hackystat.org";
  private String teamName = "Your1stTeam";
  private String formName = "DeleteTeamForm";
  
  /**
   * Tests the TeamsManageDeletePage.
   * @throws Exception If there is a bug.
   */
  @Test
  public void testDeleteTeam() throws Exception {
    WicketTester tester = new WicketTester(new DevcathlonApplication());
    makeTeam(teamName, "admin", "member1", "member2");
    
    TeamManager.getInstance().getTeam(teamName).setProjectName("Default");
    TeamManager.getInstance().getTeam(teamName).setDescription("Hello");
    TeamManager.getInstance().getTeam(teamName).setTeamLogoPath("Team1.png");
    
    tester.startPage(TeamsManageDeletePage.class);
    tester.assertRenderedPage(LoginPage.class);
    
    // Fill out the login form.
    FormTester formTester = tester.newFormTester("loginForm");
    formTester.setValue("user", user);
    formTester.setValue("password", user);
    formTester.submit();
       
    ((DevcathlonSession)tester.getWicketSession()).getTeamsSession().setSelectedTeam(teamName);
    
    tester.startPage(TeamsManageDeletePage.class);
    tester.assertRenderedPage(TeamsManageDeletePage.class);
    
    assertNotNull("wicket form found", tester.getTagByWicketId(formName));
        
    FormTester deleteForm = tester.newFormTester(formName);
    deleteForm.submit("CancelButton");    
    
    tester.assertRenderedPage(TeamsManagePage.class);
    tester.assertContains("Your1stTeam");
            
  }
  
  /**
   * Tests canceling a delete team request.
   * @throws Exception If there is a bug.
   */
  @Test
  public void testCancelTeamDelete() throws Exception {
    WicketTester tester = new WicketTester(new DevcathlonApplication());

    makeTeam(teamName, "admin", "member1", "member2");
    
    TeamManager.getInstance().getTeam(teamName).setProjectName("Default");
    TeamManager.getInstance().getTeam(teamName).setDescription("Hello");
    TeamManager.getInstance().getTeam(teamName).setTeamLogoPath("Team1.png");
    
    tester.startPage(TeamsManageDeletePage.class);
    tester.assertRenderedPage(LoginPage.class);
    
    // Fill out the login form.
    FormTester formTester = tester.newFormTester("loginForm");
    formTester.setValue("user", user);
    formTester.setValue("password", user);
    formTester.submit();
       
    ((DevcathlonSession)tester.getWicketSession()).getTeamsSession().setSelectedTeam(teamName);
    
    tester.startPage(TeamsManageDeletePage.class);
    tester.assertRenderedPage(TeamsManageDeletePage.class);
    
    assertNotNull("wicket form found", tester.getTagByWicketId(formName));
        
    FormTester deleteForm = tester.newFormTester(formName);
    deleteForm.submit();    
    
    tester.assertRenderedPage(TeamsManagePage.class);
    tester.assertContains("You are not apart of any teams");
  }
  
  
  
}
